package com.zuoshenge.main.service;

import com.zuoshenge.bean.JwtProperties;
import com.zuoshenge.main.entity.Student;
import com.zuoshenge.redis.util.RedisUtil;
import com.zuoshenge.util.JwtTokenUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class OnlineUserService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @Resource
    private JwtProperties jwtProperties;


    public void saveStudent(Student student, String token){
        String loginKey = jwtTokenUtil.loginStudentKey(token);
        redisUtil.set(loginKey, student.getSid(), jwtProperties.getTokenValidityInSeconds(), TimeUnit.MILLISECONDS);
    }

    public String queryOnlineSid(String sid) {
        String loginKey = jwtTokenUtil.loginStudentKey(sid);
        return (String) redisUtil.get(loginKey);
    }

    public void kickOutForSid(String sid) {
        String loginKey = jwtTokenUtil.loginStudentKey(sid);
        redisUtil.del(loginKey);
    }

    public Long getPunishmentTtl(String username) {
        String punishmentKry = jwtTokenUtil.punishmentKey(username);
        return redisUtil.getExpireTtl(punishmentKry, TimeUnit.MILLISECONDS);
    }

    public void studentLogout(String token) {
        String loginKey = jwtTokenUtil.loginStudentKey(token);
        redisUtil.del(loginKey);
    }
}
